package com.xmut.mall.product.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.xmut.mall.product.annotation.CheckJwt;
import com.xmut.mall.product.feign.OrderFeignService;
import com.xmut.mall.product.service.AttrService;
import com.xmut.mall.product.service.SpuInfoService;
import com.xmut.mall.product.utils.JWTUtils;
import com.xmut.mall.product.utils.RespBean;
import com.xmut.mall.product.vo.OssTestVo;
import com.xmut.mall.product.vo.TestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("product/test")
public class TestController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private OrderFeignService orderFeignService;

    @GetMapping("/getToken")
    public RespBean getToken(){
        String info = "hello world";

        String accessToken = JWTUtils.createAccessToken(info);
        String refreshToken = JWTUtils.createRefreshToken(info);

        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("accessToken",accessToken);
        tokenMap.put("refreshToken",refreshToken);
        return RespBean.success("获取成功",tokenMap);
    }

    @CheckJwt
    @PostMapping("/checkJwt")
    public RespBean checkJwt(){
        String userInfo = request.getAttribute("userInfo").toString();
        return RespBean.success("验证成功",userInfo);
    }

    @CheckJwt
    @PostMapping("/feignJwt")
    public RespBean testFeignJwt(){
        RespBean respBean = orderFeignService.testFeignJwt();
        System.out.println(respBean);
        return respBean.success("成功",respBean.getObj());
    }

    @CheckJwt
    @PostMapping("/testPostJwt")
    public RespBean testPostJwt(@RequestBody TestVo vo){
        return RespBean.success("请求成功",vo);
    }

    @PostMapping("/ossTest")
    public void ossTest(OssTestVo vo, MultipartFile file){
        System.out.println(vo);
    }
}
